package com.itheima.test3;

import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

public class Test {
    public static void main(String[] args) throws IOException {
//        需求：编写程序，使用UDP协议的发送端给接收端发送一句话：
// “服务端你好啊！”；然后接收端给发送端返回一句话，内容随意。


        DatagramSocket socket = new DatagramSocket(9999);
        int port=8888;
        InetAddress address = InetAddress.getByName("127.0.0.1");
        String s="服务端你好啊!";
        byte[] bytes = s.getBytes();
        DatagramPacket packet = new DatagramPacket(bytes,bytes.length,address,port);
        socket.send(packet);
       // socket.close();

        //DatagramSocket socket1 = new DatagramSocket(9000);
        DatagramPacket otherPacket=new DatagramPacket(new byte[1024],1024);
        socket.receive(otherPacket);
        System.out.println(new String(otherPacket.getData(),0,otherPacket.getLength()));
        socket.close();
    }
}
